const db = require('../../../db/index')
const { resContError, resContSuccess } = require('../../responseControl')
const userInfoMap = require('../../../utils/map/userInfo')
// console.log(usernameMap, typeof usernameMap);
usernameMap = userInfoMap.username
hello = async (ctx, next) => {
  resContSuccess(ctx, 'hello')
}
login = async (ctx, next) => {
  let { username, password } = ctx.request.body
  if (!username || !password) {
    resContError(ctx, '请输入用户名和密码')
    return false
  }
  try {
    let result = await db.query(`select password from user_info where username = '${username}'`)
    if (result[0]?.password === password) {
      resContSuccess(ctx, '登陆成功')
    } else {
      resContError(ctx, '账号名或密码错误')
    }
  } catch (error) {
    console.log(error);
    resContError(ctx, '登录失败')
  }

}
register = async (ctx, next) => {
  let { username, password, phone } = ctx.request.body
  username = username.replace(/\s/g, "")
  password = password.replace(/\s/g, "")
  phone = phone.replace(/\s/g, "")
  //是否符合正则
  // if (!usernameMap.reg.test(username)) {}
  // if (!usernameMap.reg.test(username)) {}
  // if (!usernameMap.reg.test(username)) {}


  let result = await db.query(`insert into user_info (username,password,phone) values ('${username}','${password}','${phone.trim()}')`)
  resContSuccess(ctx, result.insertId)
  // if (!msg.username) { }
  // if (!msg.password) { resContError(ctx, '密码不能为空') }
  // if (!msg.phonecode) { resContError(ctx, '手机号不能为空') } else { }
  // if (!msg.role) { resContError(ctx, '角色不能为空') }
}
module.exports = {
  hello, login, register
}